﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_news : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadNhomTin();
            LoadNewsByCateID(Convert.ToInt32(drdnhomtin.SelectedValue));
        }
    }
    protected void LoadNhomTin()
    {
        drdnhomtin.DataSource = from o in db.Cates where o.TypeCateID == 1 && o.Parent == 0 select o;
        drdnhomtin.DataTextField = "Name";
        drdnhomtin.DataValueField = "Cate_ID";
        drdnhomtin.DataBind();
    }
    protected void LoadNewsByCateID(int CateID)
    {
        rptnews.DataSource = from o in db.News where o.Cate_ID == CateID select o;
        rptnews.DataBind();
        System.Threading.Thread.Sleep(800);
    }
    protected void UpdateCheck(object s, RepeaterCommandEventArgs e)
    {
        if (e.CommandName=="edit")
        {
            Label lblid = (Label) e.Item.FindControl("lblid");
            HttpCookie aCookie=new HttpCookie("ckNewsID");
            aCookie.Value = lblid.Text;
            aCookie.Expires = DateTime.Now.AddHours(6);
            Response.Cookies.Add(aCookie);
            Response.Redirect("edit-news.aspx");
        }
    }
    protected void drdnhomtin_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadNewsByCateID(Convert.ToInt32(drdnhomtin.SelectedValue));
    }
    protected void DeleteNews(object s,EventArgs e)
    {
        foreach (RepeaterItem item in rptnews.Items)
        {
            CheckBox chk = (CheckBox) item.FindControl("chk");
            if (chk.Checked)
            {
                Label lblid = (Label) item.FindControl("lblid");
                var news =
                    (from o in db.News where o.NewsID == Convert.ToDecimal(lblid.Text) select o).SingleOrDefault();
                db.News.DeleteOnSubmit(news);
                db.SubmitChanges();
            }
        }
        lblthongbao.Text = "Xóa thành công";
        LoadNewsByCateID(Convert.ToInt32(drdnhomtin.SelectedValue));
    }
}